package com.li.taskcenter.service.quartz.impl;

import com.li.configserve.domain.common.CurrentUser;
import com.li.configserve.domain.gloabreturn.ResponseInfo;
import com.li.taskcenter.domain.quartz.QuartzBean;
import com.li.taskcenter.domain.taskcenter.TaskInfo;
import com.li.taskcenter.mapper.taskcenter.TaskDefineMapper;
import com.li.taskcenter.service.quartz.QuartzService;
import com.li.taskcenter.utils.QuartzUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/12/22
 * @Version：1.0.0
 */
@Service
public class QuartzServiceImpl implements QuartzService {

    private static final Logger logger = LoggerFactory.getLogger(QuartzServiceImpl.class);

    @Autowired
    private TaskDefineMapper taskDefineMapper;

    /**
     * @Author LiDong
     *  创建一个任务
     * @Date 20:45 2020/12/22
     * @Param [quartzBean, paramMap, currentUser]
     * @return com.li.configserve.domain.gloabreturn.ResponseInfo
     **/
    @Override
    public ResponseInfo createJob(Scheduler scheduler, Map<String, String> paramMap, CurrentUser currentUser) {
        ResponseInfo result = new ResponseInfo();
        try {
            TaskInfo taskInfo=getTaskInfo(paramMap,currentUser,result);
            if(Objects.nonNull(taskInfo)){
                // 定义QuartzBean
                QuartzBean quartzBean = new QuartzBean();
                // 定义任务执行的类
                quartzBean.setJobClass("com.li.taskcenter.controller.quartz.MyTask");
                // 定义任务的名称唯一
                quartzBean.setJobName(taskInfo.getTaskcode());
                // corn表达式
                quartzBean.setCronExpression(taskInfo.getCorn());
                QuartzUtils.createScheduleJob(scheduler,quartzBean);
            }
            return result;
        } catch (Exception e) {
            logger.info(e.getMessage());
            return result;
        }
    }

    private TaskInfo getTaskInfo(Map<String, String> paramMap, CurrentUser currentUser, ResponseInfo result) {
        // 任务定义Id
        String taskdefineid = paramMap.getOrDefault("taskdefineid", null);
        if(StringUtils.isEmpty(taskdefineid)){
            result.setCode(500);
            result.setMessage("任务定义Id为空!");
        }
        Map<String,String> map = new HashMap<>(1);
        map.put("taskdefineid",taskdefineid);
        return taskDefineMapper.getTaskDefine(map);
    }

    /**
     * @Author LiDong
     * @Description // 停用一个任务
     * @Date 20:45 2021/1/5
     * @Param [scheduler, paramMap, currentUser]
     * @return com.li.configserve.domain.gloabreturn.ResponseInfo
     **/
    @Override
    public ResponseInfo pauseJob(Scheduler scheduler, Map<String, String> paramMap, CurrentUser currentUser) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            TaskInfo taskInfo=getTaskInfo(paramMap,currentUser,responseInfo);
            if(Objects.nonNull(taskInfo)){
                String taskcode = taskInfo.getTaskcode();
                QuartzUtils.pauseScheduleJob (scheduler,taskcode);
                responseInfo.setCode(200);
                responseInfo.setMessage(taskcode+"任务暂停成功!");
                return responseInfo;
            }
            responseInfo.setMessage("当前任务暂停失败！");
            return responseInfo;
        } catch (Exception e) {
            responseInfo.setCode(500);
            responseInfo.setMessage(e.getMessage());
            return responseInfo;
        }
    }

    @Override
    public ResponseInfo runOnce(Scheduler scheduler, Map<String, String> paramMap, CurrentUser currentUser) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            TaskInfo taskInfo=getTaskInfo(paramMap,currentUser,responseInfo);
            if(Objects.nonNull(taskInfo)) {
                String taskcode = taskInfo.getTaskcode();
                QuartzUtils.runOnce (scheduler,taskcode,taskInfo);
            }
            responseInfo.setMessage("运行一次成功！");
            responseInfo.setCode(200);
            return responseInfo;
        } catch (Exception e) {
            responseInfo.setCode(500);
            responseInfo.setMessage(e.getMessage());
            return responseInfo;
        }
    }

    @Override
    public ResponseInfo resumeScheduleJob(Scheduler scheduler, Map<String, String> paramMap, CurrentUser currentUser) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            TaskInfo taskInfo=getTaskInfo(paramMap,currentUser,responseInfo);
            if (Objects.nonNull(taskInfo)) {
                String taskcode = taskInfo.getTaskcode();
                QuartzUtils.resumeScheduleJob(scheduler, taskcode);
            }
            return responseInfo;
        }catch (Exception e){
            return responseInfo;
        }
    }
}
